import { ElMessage } from 'element-plus';
import { useEditScripts } from '/@/hooks/useEditScripts';

const ctxinitialState = () => ({
	isEdit: false,
});
// 使用 reactive 初始化
const ctx = reactive(ctxinitialState());

const { openDrawerFn, closeDrawerFn, mkDrawerRef } = useEditScripts<any>();

export const initialize = (formValue: globalThis.Ref<any, any>, displayData: Function) => {
	//初始化表单的值
	const defaultValue = () => {
		formValue.value = {
			id: undefined,
			name: undefined,
			projectCode: undefined,
			textCode: undefined,
			referenceRange: undefined,
			upperLimit: undefined,
			lowerLimit: undefined,
			explosivePopularity: undefined,
			unit: undefined,
			xoc: undefined,
			sco: undefined,
			defaultValue: undefined,
			enumType: undefined,
			instrumentTask: undefined,
			remark: undefined,
			isEnabled: false,
		};
	};

	const submitFn = () => {
		ElMessage({
			message: '保存成功',
			type: 'success',
		});
		ctx.isEdit = false;
		closeDrawerFn();
		displayData();
	};

	//数据赋值
	const dataAssignment = (row: any) => {
		if (row) {
			formValue.value = JSON.parse(JSON.stringify(row));
		}
	};

	return { ctx, submitFn, openDrawerFn, closeDrawerFn, dataAssignment, mkDrawerRef, formValue, defaultValue };
};
